import 'package:farmer/model/video_model.dart';
import 'package:farmer/ui/pages/home_page.dart';
import 'package:farmer/ui/pages/login_page.dart';
import 'package:farmer/ui/pages/registration_page.dart';
import 'package:farmer/ui/pages/video_detail_page.dart';
import 'package:farmer/util/color.dart';
import 'package:flutter/material.dart';
import 'db/hi_cache.dart';
import 'http/core/hi_error.dart';
import 'http/dao/login_dao.dart';
import 'util/pixel_adapter.dart';

void main() {
  runApp(FpayApp());
  PixelAdapter.initWidth(1242);
}

class FpayApp extends StatefulWidget {


  const FpayApp({Key key}) : super(key: key);

  @override
  _FpayAppState createState() => _FpayAppState();
}

class _FpayAppState extends State<FpayApp> {
  FpayRouteDelete _fpayRouteDelete= FpayRouteDelete();
  FpayRoutInfomationParser _fpayRoutInfomationParser= FpayRoutInfomationParser();

  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    //定义Route
    var widget=Router(
      routeInformationParser: _fpayRoutInfomationParser,
      routerDelegate: _fpayRouteDelete,
      routeInformationProvider: PlatformRouteInformationProvider(
        initialRouteInformation: RouteInformation(location: "/")
      ),
    );
    return MaterialApp(
      home: widget,
      debugShowCheckedModeBanner: false,
    );
  }
}

class FpayRouteDelete extends RouterDelegate<FpayRoutePath>
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<FpayRoutePath> {
  List<MaterialPage> pages = [];
  VideoModel videoModel;
  FpayRoutePath path;

  @override
  GlobalKey<NavigatorState> get navigatorKey => GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    pages = [
      pageWrap(HomePage(onJumpToDetail: (videoModel){
        this.videoModel=videoModel;
        notifyListeners();
      },)),
      if (videoModel != null) pageWrap(VideoDetailPage(videoModel))
    ];
    return Navigator(
        key: navigatorKey,
        pages: pages,
        onPopPage: (route, result) {
          if (!route.didPop(result)) {
            return false;
          }
          return true;
        });
  }

  @override
  Future<void> setNewRoutePath(FpayRoutePath path) async {
    this.path = path;
  }
}

class FpayRoutInfomationParser extends RouteInformationParser<FpayRoutePath> {
  @override
  Future<FpayRoutePath> parseRouteInformation(
      RouteInformation routeInformation) async {
    final uri = Uri.parse(routeInformation.location);
    print('uri: $uri');
    if (uri.pathSegments.length == 0) {
      return FpayRoutePath.home();
    }
    return FpayRoutePath.detail();
  }
}

//创建页面
pageWrap(Widget child) {
  return MaterialPage(key: ValueKey(child.hashCode), child: child);
}

///定义路由数据，path
class FpayRoutePath {
  final String location;

  FpayRoutePath.home() : location = "/";

  FpayRoutePath.detail() : location = "/detail";
}
